#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/.
"""Jessyink action extension."""

import inkex
from jessyInk_c1_install import JessyInkMixin, _

class JessyinkEffects(JessyInkMixin, inkex.EffectExtension):
    def add_arguments(self, pars):
        pars.add_argument('--tab')
        pars.add_argument('--actiontype', type=str, default='none')
        pars.add_argument('--jumpslide', type=str, default='0')
        pars.add_argument('--jumpeffect', type=str, default='0')

    def effect(self):
        # Check version.
        self.is_installed()
        if not self.svg.selection:
            raise inkex.AbortExtension(
                _("No object selected. Please select the object you want to "
                  "assign an effect to and then press apply.\n"))

        for id, node in self.svg.selection.items():
            if (self.options.actiontype == "none"):
                # Remove possible action.
                if "onmousedown" in node.attrib:
                    del node.attrib["onmousedown"]
                if "onclick" in node.attrib:
                    del node.attrib["onclick"]
                if "ondblclick" in node.attrib:
                    del node.attrib["ondblclick"]
            if (self.options.actiontype == "drag"):
                node.set("onmousedown","mydrag(evt)")
            if (self.options.actiontype == "jump"):
                node.set("onclick","jumpto(" + self.options.jumpslide + "," + self.options.jumpeffect + ")")
            if (self.options.actiontype == "fold"):
                if "transform" in node.attrib:
                    # inkex.errormsg(_(node.attrib["transform"])) #''.join(node.attrib.values())
                    s_matrix = node.attrib["transform"]
                    if "matrix" in s_matrix:
                        node.set("onclick","this.style.transform='scale(1,1)'")
                        node.set("ondblclick","this.style.transform=''")
                        return
                #else:
                node.set("onmousedown","mydrag(evt, 1)")
            if (self.options.actiontype == "opacity"):
                node.set("ondblclick","this.style.fillOpacity=1-this.style.fillOpacity")

# Create effect instance
if __name__ == '__main__':
    JessyinkEffects().run()
